package gui;

import gui.mvc.Controller;
import gui.mvc.View;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;

import api.storage.DataStorageCSV;
import api.storage.DataStorageExcel;
import api.storage.DataStorageXML;
import api.storage.IDataStorage;

/**
 * @author Ann Raboschuk
 */
public class AppClient
{
   private static final int  DEFAULT_WIDTH    = 700;
   private static final int  DEFAULT_HEIGHT   = 250;
   private static FileFilter[] FILTERS;
   private JFrame frame;
   private Controller controller;
   private View view;
   private JFileChooser chooser;
   
   static
   {
      FILTERS = new FileFilter[3];
      FILTERS[0] = new FileFilter()
      {
         @Override
         public boolean accept(File f)
         {
            return f.getName().toLowerCase().endsWith(".txt")
                  || f.isDirectory();
         }

         @Override
         public String getDescription()
         {
            return "TXT file";
         }
      };
      
      FILTERS[1] = new FileFilter()
      {
         @Override
         public boolean accept(File f)
         {
            return f.getName().toLowerCase().endsWith(".xml")
                  || f.isDirectory();
         }

         @Override
         public String getDescription()
         {
            return "XML file";
         }
      };
      
      FILTERS[2] = new FileFilter()
      {
         @Override
         public boolean accept(File f)
         {
            return f.getName().toLowerCase().endsWith(".xls")
                  || f.isDirectory();
         }

         @Override
         public String getDescription()
         {
            return "Excel file";
         }
      };
   }
   
   public AppClient()
   {
      frame = new JFrame("Persons");
      frame.setResizable(false);
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setBounds((screenSize.width - DEFAULT_WIDTH) / 2,
            (screenSize.height - DEFAULT_HEIGHT) / 2, DEFAULT_WIDTH, DEFAULT_HEIGHT);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      controller = new Controller();
      view = new View(controller);
      chooser = new JFileChooser();
      chooser.setCurrentDirectory(new File("."));
      frame.setJMenuBar(createMenu());
      frame.setContentPane(view);
   }
   
   public JMenuBar createMenu()
   {     
      JMenuBar menuBar = new JMenuBar();
      String[] subMenus = {"CSV...", "XML...", "Excel..."};
      ActionListener listener = new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent event)
         {
            switch(event.getActionCommand().charAt(0))
            {
            case 'C'://CSV-file
               chooser.setFileFilter(FILTERS[0]);
               break;
            case 'X'://XML-file
               chooser.setFileFilter(FILTERS[1]);
               break;
            case 'E'://Excel-file
               chooser.setFileFilter(FILTERS[2]);
               break;
            }
            if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
            {
               try
               {
                  IDataStorage ds = null;
                  switch(event.getActionCommand().charAt(0))
                  {
                  case 'C'://CSV-file
                     ds = new DataStorageCSV(chooser.getSelectedFile().getAbsolutePath());
                     break;
                  case 'X'://XML-file
                     ds = new DataStorageXML(chooser.getSelectedFile().getAbsolutePath());
                     break;
                  case 'E'://Excel-file
                     ds = new DataStorageExcel(chooser.getSelectedFile().getAbsolutePath());
                     break;
                  }
                  if (event.getActionCommand().charAt(1) == 'L')//load command
                     controller.setPeople(ds.load());
                  else
                     ds.save(controller.getPeople());//save command
                  view.redraw();
               }
               catch (Exception ex)
               {
                  ex.printStackTrace();
               }
            }
         }
      };
      
      JMenu loadMenu = new JMenu("Load");
      loadMenu.setMnemonic('L');
      JMenu saveMenu = new JMenu("Save");
      saveMenu.setMnemonic('S');
      for (int i=0; i<subMenus.length; i++)
      {
         JMenuItem loadItem = new JMenuItem(subMenus[i]);
         loadItem.addActionListener(listener);
         loadItem.setActionCommand(subMenus[i].charAt(0)+"L");
         loadMenu.add(loadItem);
         
         JMenuItem saveItem = new JMenuItem(subMenus[i]);
         saveItem.addActionListener(listener);
         saveItem.setActionCommand(subMenus[i].charAt(0)+"S");
         saveMenu.add(saveItem);
      }
      menuBar.add(loadMenu);
      menuBar.add(saveMenu);
      
      JMenu menu = new JMenu("About");
      menu.addMouseListener(new MouseAdapter()
      {
         @Override
         public void mousePressed(MouseEvent e)
         {
            JOptionPane.showMessageDialog(frame,
                  "This programm is the product of Raboshuk&Uryadkina work", "About",
                  JOptionPane.INFORMATION_MESSAGE);
         }
      });
      menuBar.add(menu);
      return menuBar;
   }
   
   public static int getDefaultWidth()
   {
      return DEFAULT_WIDTH;
   }
   
   public void show()
   {
      frame.setVisible(true);
   }
   
//   public static void main(String[] args)
//   {
//      
//      	
//      	
//      	new AppClient().show();
//   }
}
